import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sptapp/basic/base_info.dart';

import '../../api/interface/user_api.dart';
import '../../api/request/response_data.dart';
import '../../config/const.dart';
import '../../moudle/common_info.dart';
import '../../moudle/event_bus.dart';
import '../../widget/common_widget.dart';

class MinAddDeviceSuccess extends StatefulWidget {
  final String sn;
  const MinAddDeviceSuccess({super.key, required this.sn});

  @override
  State<MinAddDeviceSuccess> createState() => _MinAddDeviceSuccessState();
}

class _MinAddDeviceSuccessState extends State<MinAddDeviceSuccess> {

  _addDevice()async{
    Map<String,dynamic> data = {
      'sn': widget.sn
    };
    showLoading();
    ResponseData res = await UserAPI.addDevice(data);
    hideLoading();
    if(res.code == 200){
      LocalBaseInfo.setDeviceNum(widget.sn);
      getDevicesList((){});
      eventBus.fire(DeviceInfoChange());
      // Navigator.of(context).pop();
      Navigator.of(context).popUntil(ModalRoute.withName("index"));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 48.w),
        child: Column(
          children: [
            SizedBox(height: ScreenUtil().statusBarHeight + 40.w,),
            myCustomCenterTitleAppBar('Add new device'),
            SizedBox(height: 240.w,),
            Container(
              alignment: Alignment.center,
              child: Column(
                children: [
                  Container(
                    child: Image.asset('assets/home/min_spt.png',width: 140.w,fit: BoxFit.fitWidth,),
                  ),
                  GestureDetector(
                    onTap: (){
                    },
                    child: Container(
                      child: Text(widget.sn,style: TextStyle(
                          color: Colors.white,
                          fontSize: 32.sp,
                        fontWeight: FontWeight.w600
                      ),),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(height: 180.w,),
            GestureDetector(
              onTap: (){
                _addDevice();
              },
              child: Container(
                width: 654.w,
                child: Text('Save',style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
                decoration: BoxDecoration(
                    color:  themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                height: 84.w,
                alignment: Alignment.center,
              ),
            ),
            // GestureDetector(
            //   onTap: (){
            //   },
            //   child: Container(
            //     width: 654.w,
            //     child: Text('Modify the device name',style: TextStyle(
            //         color: Colors.white,
            //         fontSize: 28.sp,
            //       fontWeight: FontWeight.w600
            //     ),),
            //     decoration: BoxDecoration(
            //         color:  Color(0xff2A2A2A),
            //         borderRadius: BorderRadius.circular(8.w)
            //     ),
            //     height: 84.w,
            //     alignment: Alignment.center,
            //   ),
            // ),
          ],
        ),
      ),
    );
  }
}
